//
//  IndexMenuList.swift
//  browser
//
//  Created by Kotori on 2021/6/9.
//
import SwiftUI
import UIKit

struct IndexMenuList: View {
    var indexCallback: IndexCallback?
    var indexBtnDelegate: IndexBtnDelegate?
    @Binding var isLoading: Bool
    @Binding var isBookmarked: Bool
    
    func refreshBookmarkStatus() {
        if let b = indexBtnDelegate?.isBookmarked() {
            isBookmarked = b
        }
    }
    
    var body: some View {
        GeometryReader{ reader in
            Menu {
                
                Button(action: {
                    indexBtnDelegate?.bookmark(isDelete: isBookmarked)
                    refreshBookmarkStatus()
                }, label: {
                    if isBookmarked {
                        Text("删除书签")
                        Image(systemName: iconSetName[1])
                    } else {
                        Text("添加到书签")
                        Image(systemName: iconSetName[0])
                    }
                    
                })
                
                if isLoading {
                    Button(action: {
                        indexBtnDelegate?.stopLoading()
                    }, label: {
                        Text("停止")
                        Image(systemName: iconSetName[3])
                    })
                } else {
                    Button(action: {
                        indexBtnDelegate?.reload()
                    }, label: {
                        Text("刷新")
                        Image(systemName: iconSetName[2])
                    })
                }
                Button(action: {
                    indexCallback?.goToView(to: .IndexView, from: .IndexView, intent: .openNewTabOnIndexView, with: nil)
                }, label: {
                    Text("新建标签页")
                    Image(systemName: iconSetName[5])
                })

                Button(action: {}, label: {
                    Text("分享")
                    Image(systemName: iconSetName[4])
                })
                
                Divider()
                
                Button("书签", action: {
                    indexCallback?.goToView(to: .BookmarkView, from: .IndexView, intent: .Default, with: nil)
                })
                Button("历史记录", action: {
                    indexCallback?.goToView(to: .HistoryView, from: .IndexView, intent: .Default, with: nil)
                })
                Button("下载内容", action: {
                    indexCallback?.goToView(to: .DownloadView, from: .IndexView, intent: .Default, with: nil)
                })
                Button("设置", action: {
                    indexCallback?.goToView(to: .SettingView, from: .IndexView, intent: .Default, with: nil)
                })
            } label: {
                Image(systemName: "ellipsis").imageScale(.large)
                    .frame(height: reader.size.height)
            }
        }
        .onAppear() {
            refreshBookmarkStatus()
        }
        .onTapGesture {
            refreshBookmarkStatus()
        }
        
        
    }
}

private let iconSetName = ["bookmark","bookmark.fill", "arrow.clockwise","xmark","square.and.arrow.up","plus"]

struct IndexMenuList_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            IndexMenuList(isLoading: .constant(false), isBookmarked: .constant(false))
        }
    }
}
